import { observable, action } from 'mobx';
import objectAssign from 'object-assign';
import { message, Modal } from 'antd';
import {WeaLocaleProvider, WeaTools} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
const { ls } = WeaTools; 
import { WeaTableNew } from 'comsMobx';
const { TableStore } = WeaTableNew;
class NewNoticeMoreStore {
    @observable tableStore = new TableStore();
    @observable status = '0'; // 状态码
    @observable sessionkey = ''; // sessionkey
    @observable logintemplatename = ''; // 当前启用的登录前门户
    @observable canAddNotice = true; // 是否可以编辑
    constructor() {
        this.getSessionKey = this.getSessionKey.bind(this);
        this.doDel = this.doDel.bind(this);
        this.deleteRows = this.deleteRows.bind(this);
        this.initParam = this.initParam.bind(this);
        this.handleRefresh = this.handleRefresh.bind(this);
    }
    @action handleRefresh(eid){
      this.initParam(eid,this.canAddNotice);
    }
    @action //恢复默认
    resetDefaultSetting(){
        if(this.state.templateType === 'E9'){
            this.login4e9_setting_store.resetDefaultSetting();
        }else{
            this.login4e8_setting_store.resetDefaultSetting();
        }
    }
    @action
    initParam(eid,canAddNotice){
        this.tableStore.current = 1;
        this.getSessionKey({eid,canAddNotice});
    }
    @action //获取登录前门户列表的sessionkey
    getSessionKey(params = {}){
        if (this.sessionkey) {
            const { current } = this.tableStore;
            if (!params.current) {
                params.current = current;
            }
        }
        if(params.logintemplatename == undefined){
            params.logintemplatename = this.logintemplatename;
        }
        WeaTools.callApi('/api/portal/elementmore/getNewNoticeMoreDatas', 'POST', params).then(data => {
            const status = data.status || '1';
            if (status == '1') {
                this.canAddNotice = data.canAddNotice;
                this.sessionkey = data.sessionkey;
                this.tableStore.getDatas(data.sessionkey, params.current || 1);
            }else if(status == '2'){
                message.error(data.errormsg);
                return;
            }
            this.status = status;
        });
    }
    @action //删除数据
    doDel(ids,eid){
       Modal.confirm({
            title: getLabel(386786,"信息确认"),
            content:getLabel('15097','确定要删除吗？'),
            onOk: () => {
                WeaTools.callApi('/api/portal/NewNoticeMore/del', 'POST', {ids}).then((result) => {
                    if (result) {
                        message.success(getLabel('83472','删除成功！'));
                        this.initParam(eid,this.canAddNotice);
                    } else {
                        message.error(getLabel('83473','删除失败！'));
                    }
                });
            },
        });
    }
    @action //批量删除数据 
    deleteRows(eid){
        let ids = "";
        if(this.sessionkey){
            const { selectedRowKeys } = this.tableStore;
            ids = selectedRowKeys.join(",");
        }
        this.doDel(ids,eid);
        this.initParam(eid,this.canAddNotice);
    }
}
module.exports = NewNoticeMoreStore;
